#pragma once
#include "pch.h"
#include "Direct3DApplication.h"

void Direct3DApplication::Initialize(CoreWindow^ window)
{
	coreWindow = window;
}

void Direct3DApplication::CreateDeviceAndSwapChain()
{
	UINT deviceCreationFlags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;
	//If debug add |= D3D11_CREATE_DEVICE_DEBUG;

	D3D_FEATURE_LEVEL* featureLevel = nullptr;
	ComPtr<ID3D11Device> tmpDevice;
	ComPtr<ID3D11DeviceContext> tmpContext;

	D3D11CreateDevice(
		nullptr,
		D3D_DRIVER_TYPE_HARDWARE,
		nullptr,
		deviceCreationFlags,
		nullptr,
		0,
		D3D11_SDK_VERSION,
		&tmpDevice,
		featureLevel,
		&tmpContext);

	tmpDevice.As(&d3dDevice);
	tmpContext.As(&d3dContext);

	//Create the swap chain:
	DXGI_SWAP_CHAIN_DESC1 scDesc = {0};

	Rect windowBounds = this->coreWindow->Bounds;
	Size windowSize;

	windowSize.Width = windowBounds.Width;
	windowSize.Height = windowBounds.Height;

	scDesc.Width = static_cast<UINT>(windowSize.Width);
	scDesc.Height = static_cast<UINT>(windowSize.Height);

	scDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
	scDesc.Stereo = false;
	scDesc.SampleDesc.Count = 1;
	scDesc.SampleDesc.Quality = 0;
	scDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
	scDesc.BufferCount = 2;
	scDesc.Scaling = DXGI_SCALING_NONE;
	scDesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL;
	scDesc.Flags = 0;

	ComPtr<IDXGIDevice1> dxgiDevice;
	d3dDevice.As(&dxgiDevice);
	ComPtr<IDXGIAdapter> dxgiAdapter;
	dxgiDevice->GetAdapter(&dxgiAdapter);
	ComPtr<IDXGIFactory2> dxgiFactory;
	dxgiAdapter->GetParent(__uuidof(IDXGIFactory2), &dxgiFactory);

	dxgiFactory->CreateSwapChainForCoreWindow(
		d3dDevice.Get(),
		reinterpret_cast<IUnknown*>(coreWindow),
		&scDesc,
		nullptr, //Not needed for now.
		&dxgiSwapChain);

}

Direct3DApplication::Direct3DApplication()
{
	Initialize(CoreWindow::GetForCurrentThread());

	CreateDeviceAndSwapChain();
}